package com.jinlingxiu.app.mvp.ui.activity

import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.LogUtils
import com.jaeger.library.StatusBarUtil
import com.jinlingxiu.app.R
import com.jinlingxiu.app.mvp.contract.NoPassContract
import com.jinlingxiu.app.mvp.presenter.NoPassPresenter
import com.jinlingxiu.app.other.adapter.NoPassAdapter
import com.jinlingxiu.app.other.bean.ErrorBean
import com.jinlingxiu.app.other.widgets.LoadingDialogBehavior
import com.xdja.easymvp.base.BaseActivity
import kotlinx.android.synthetic.main.activity_no_pass.*
import org.jetbrains.anko.toast
import org.koin.androidx.scope.lifecycleScope


/**
 * 描述:  测试未通过页
 * Create by yuanwanli
 * Date 2020/09/11
 */
class NoPassActivity : BaseActivity<NoPassPresenter>(), NoPassContract.View {
    override var mPresenter: NoPassPresenter? = lifecycleScope.get()
    lateinit var adapter: NoPassAdapter
    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_no_pass
    var errors: ArrayList<ErrorBean>? = null
    private val mediaPlayer = MediaPlayer()
    private val materialDialog: MaterialDialog by lazy {
        MaterialDialog(
            this,
            dialogBehavior = LoadingDialogBehavior()
        ).customView(R.layout.dialog_progress1)
    }

    override fun initData(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, lvTop)
        StatusBarUtil.setLightMode(this)
        errors = intent.getParcelableArrayListExtra("data")
        adapter = NoPassAdapter(mRecyclerView)
        adapter.data = errors
        adapter.setOnItemChildClickListener { _, _, position ->
            adapter.restoreAllData(position)
            val item = adapter.getItem(position)
            playVoice(item, position)
        }
        btnSolution.setOnClickListener {
            materialDialog.show()
            mPresenter?.getSolution()
        }
        ivBack.setOnClickListener {
            ActivityUtils.finishAllActivities()
            startActivity(Intent(this, LoginActivity::class.java))
        }
    }

    private fun playVoice(errorBean: ErrorBean, position: Int) {
        val fileExists = FileUtils.isFileExists(errorBean.soundPath)
        if (!fileExists) {
            toast("录音文件不存在")
            return
        }
        if (!errorBean.playing) {
            return
        }
        try {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.stop()
            }
            mediaPlayer.reset()
            mediaPlayer.setDataSource(errorBean.soundPath)//指定音频文件路径
            mediaPlayer.isLooping = false//设置为循环播放
            mediaPlayer.prepare()//初始化播放器MediaPlayer
            mediaPlayer.start()
            mediaPlayer.setOnCompletionListener {
                adapter.restoreAllData(position)
            }
        } catch (t: Throwable) {
            LogUtils.e(t)
        }
    }

    override fun jumpSolution() {
        materialDialog.dismiss()
        val intent = Intent(this, SolutionActivity::class.java)
        intent.putParcelableArrayListExtra("data", errors)
        startActivity(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        mediaPlayer.stop()
        mediaPlayer.release()
    }
}
